import { ref } from 'vue';

import { defineStore } from 'pinia';

import { list } from '#/api/infra/config';

/**
 * 地图配置存储
 */
export const useMapStore = defineStore('map', () => {
  // 地图配置
  const mapConfig = ref<Record<string, string>>({
    amap_key: '',
    amap_security_js_code: '',
    amap_version: '2.0',
    amap_plugins: 'AMap.Geocoder,AMap.PlaceSearch,AMap.Marker',
    amap_default_city: '全国',
    amap_default_zoom: '13',
    map_provider: 'amap', // 新增：地图服务商，支持 amap, baidu, tencent
    baidu_map_key: '', // 百度地图密钥
    tencent_map_key: '', // 腾讯地图密钥
  });

  const loading = ref(false);

  /**
   * 获取地图配置
   */
  const getMapConfig = async () => {
    loading.value = true;
    try {
      const data = await list({ category: 'MAP', pageNo: 1, pageSize: 100 });
      if (data && data.length > 0) {
        data.forEach((item: any) => {
          if (item.key && item.value !== undefined) {
            mapConfig.value[item.key] = item.value;
          }
        });
      } else {
        // 如果没有获取到配置数据，使用默认配置
        console.warn('未获取到地图配置数据，使用默认配置');
        mapConfig.value = {
          amap_key: '',
          amap_security_js_code: '',
          amap_version: '2.0',
          amap_plugins: 'AMap.Geocoder,AMap.PlaceSearch,AMap.Marker',
          amap_default_city: '全国',
          amap_default_zoom: '13',
          map_provider: 'amap',
          baidu_map_key: '',
          tencent_map_key: '',
        };
      }
    } catch (error) {
      console.error('获取地图配置失败:', error);
      // 在错误情况下也使用默认配置
      mapConfig.value = {
        amap_key: '',
        amap_security_js_code: '',
        amap_version: '2.0',
        amap_plugins: 'AMap.Geocoder,AMap.PlaceSearch,AMap.Marker',
        amap_default_city: '全国',
        amap_default_zoom: '13',
        map_provider: 'amap',
        baidu_map_key: '',
        tencent_map_key: '',
      };
    } finally {
      loading.value = false;
    }
    return mapConfig.value;
  };

  /**
   * 获取高德地图API Key
   */
  const getAmapKey = () => {
    return mapConfig.value.amap_key || '';
  };

  /**
   * 获取高德地图安全密钥
   */
  const getAmapSecurityJsCode = () => {
    return mapConfig.value.amap_security_js_code || '';
  };

  /**
   * 获取高德地图版本
   */
  const getAmapVersion = () => {
    return mapConfig.value.amap_version || '2.0';
  };

  /**
   * 获取高德地图插件
   */
  const getAmapPlugins = () => {
    return (
      mapConfig.value.amap_plugins ||
      'AMap.Geocoder,AMap.PlaceSearch,AMap.Marker'
    );
  };

  /**
   * 获取默认城市
   */
  const getDefaultCity = () => {
    return mapConfig.value.amap_default_city || '全国';
  };

  /**
   * 获取默认缩放级别
   */
  const getDefaultZoom = () => {
    return mapConfig.value.amap_default_zoom || '16';
  };

  /**
   * 获取地图服务商
   */
  const getMapProvider = () => {
    return mapConfig.value.map_provider || 'amap';
  };

  /**
   * 获取百度地图密钥
   */
  const getBaiduMapKey = () => {
    return mapConfig.value.baidu_map_key || '';
  };

  /**
   * 获取腾讯地图密钥
   */
  const getTencentMapKey = () => {
    return mapConfig.value.tencent_map_key || '';
  };

  return {
    mapConfig,
    loading,
    getMapConfig,
    getAmapKey,
    getAmapSecurityJsCode,
    getAmapVersion,
    getAmapPlugins,
    getDefaultCity,
    getDefaultZoom,
    getMapProvider,
    getBaiduMapKey,
    getTencentMapKey,
  };
});
